package ast;

import compiler.OutputFormatter;

public class ReadStatement extends Statement {

    private Variable variable;

    public ReadStatement(Variable variable) {
        setVariable(variable);
    }

    @Override
    public void genC(OutputFormatter of) {
        if (variable.getType() == Type.INT)
            /* The getchar() call was needed to skip the \n */
            of.output("scanf(\"%d\", &" + variable.getName() + "); getchar()");
        else
            of.output("string_read(" + variable.getName() + ")");
        super.genC(of);
    }

    public Variable getVariable() {
        return variable;
    }

    public void setVariable(Variable variable) {
        this.variable = variable;
    }
}
